package com.xiaozhengkeji.minecraft.plugin.fishplus.listener;

import com.xiaozhengkeji.minecraft.plugin.fishplus.base.MemoryBase;
import com.xiaozhengkeji.minecraft.plugin.fishplus.config.FishConfig;
import com.xiaozhengkeji.minecraft.plugin.fishplus.entity.FishPrizeEntity;
import com.xiaozhengkeji.minecraft.plugin.fishplus.entity.FishRodEntity;
import com.xiaozhengkeji.minecraft.plugin.fishplus.entity.PlayerDataEntity;
import com.xiaozhengkeji.minecraft.plugin.fishplus.utils.FishCoreUtils;
import com.xiaozhengkeji.minecraft.plugin.fishplus.utils.MsgUtils;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerFishEvent;
import org.bukkit.inventory.ItemStack;

import java.util.Random;


/**
 * 定制化玩家钓鱼监听器
 * 1.用来监听不同玩家的钓鱼事件
 * <p>
 * <p>
 * <p>
 * 实现原理
 * <p>
 * 1.玩家进服创建订制器
 * 2.玩家使用鱼竿开始钓鱼
 * 2.1 获取玩家鱼竿类型
 * 2.2 获取玩家当前幸运值 (鱼竿幸运值 + 历史幸运值 + 独特Buff加成卡)
 * 2.3 随机创建并且获取本次奖品
 * 2.4 设置玩家本次钓鱼成功概率
 * 3.玩家收杆
 * 1.判断是否成功
 * 2.进行奖励发放
 * 3.如果成功钓到指定幸运值以上物品 即清空历史幸运值
 * 4.发布钓鱼成功事件 用来数据统计
 */
public class PlayerCustomFishListener implements Listener {
    public Player ListenerPlayer;
    public FishPrizeEntity FishPrizeEntity;
    public PlayerDataEntity PlayerDataEntity;
    public FishRodEntity FishRodPojo;

    public PlayerCustomFishListener(Player listenerPlayer) {
        ListenerPlayer = listenerPlayer;
        this.PlayerDataEntity = MemoryBase.getPlayerData(listenerPlayer.getUniqueId());
    }

    /**
     * 玩家钓鱼事件
     */
    @EventHandler(ignoreCancelled = true)
    public void onPlayerFish(PlayerFishEvent event) {
        /*优先判断触发事件玩家是否是本监听器所属*/
        if (event.getPlayer().getUniqueId() != ListenerPlayer.getUniqueId()) {
            return;
        }
        /*判断当前钓鱼进度*/
        if (event.getState() == PlayerFishEvent.State.FISHING) {//玩家抛线
            onPlayerFishStateFishing(event);
        } else if (event.getState() == PlayerFishEvent.State.BITE) {//当准备上钩时
            onPlayerFishStateBite(event);
        } else if (event.getState() == PlayerFishEvent.State.FAILED_ATTEMPT) {//无法钓鱼时
            onPlayerFishStateFailedAttempt(event);
        }
    }

    /**
     * 玩家抛线事件
     */
    public void onPlayerFishStateFishing(PlayerFishEvent event) {
        Player player = event.getPlayer();
        //获取玩家手持
        ItemStack temp_player_main_item = player.getInventory().getItemInMainHand();
        if (temp_player_main_item.getItemMeta() == null) {
            MsgUtils.logPlayer(player, FishConfig.LANGUAGE_HASH_MAP.get("NoConfigurationRodWasUsedWhenFishing"));
            event.setCancelled(true);
            return;
        }

        this.FishRodPojo = FishCoreUtils.getFishRodConfigByItemLore(temp_player_main_item);
        if (this.FishRodPojo == null) {
            MsgUtils.logPlayer(player, FishConfig.LANGUAGE_HASH_MAP.get("NoConfigurationRodWasUsedWhenFishing"));
            event.setCancelled(true);
        } else {
            //设置本次的成功概率
            //event.getHook().setBiteChance(this.FishRodPojo.hit);
            //获取本次的幸运值
            this.PlayerDataEntity.MaxLucky = this.PlayerDataEntity.MaxLucky + new Random().nextInt(FishConfig.LuckyIncrease);
            this.FishPrizeEntity = FishCoreUtils.lotteryByLucky(this.PlayerDataEntity.MaxLucky);
            if (this.PlayerDataEntity.MaxLucky > FishConfig.LuckyMax) this.PlayerDataEntity.MaxLucky = 0;
            //开始钓鱼
            this.PlayerDataEntity.MaxNumFishing = this.PlayerDataEntity.MaxNumFishing + 1;
        }


    }

    /**
     * 玩家钓鱼准备上钩时
     */
    public void onPlayerFishStateBite(PlayerFishEvent event) {
        //event.setCancelled(true);
        if (this.FishPrizeEntity == null) {
            MsgUtils.logPlayer(this.ListenerPlayer, FishConfig.LANGUAGE_HASH_MAP.get("IllegalPhishing"));
        } else {
            FishCoreUtils.sendPrizes(this.FishPrizeEntity, this.ListenerPlayer);
            if (this.FishPrizeEntity.announcementEnable) {
                String announcement = FishPrizeEntity.announcementContent;
                announcement = announcement.replaceAll("<p>", this.ListenerPlayer.getDisplayName());
                announcement = announcement.replaceAll("<rod>", this.FishRodPojo.FishRodName);
                announcement = announcement.replaceAll("<prize>", this.FishPrizeEntity.prizeName);
                MsgUtils.logALLPlayer(announcement);
            }
            this.FishPrizeEntity = null;
            this.PlayerDataEntity.MaxNumFishing_Success = this.PlayerDataEntity.MaxNumFishing_Success + 1;
            MemoryBase.savePlayerData(this.PlayerDataEntity);//对玩家数据进行存储!
        }
    }

    /**
     * 玩家本次下杆无法钓鱼
     */
    public void onPlayerFishStateFailedAttempt(PlayerFishEvent event) {
        this.PlayerDataEntity.MaxLucky = this.PlayerDataEntity.MaxLucky - new Random().nextInt(FishConfig.LuckyIncrease);
        if (this.PlayerDataEntity.MaxLucky < 0) this.PlayerDataEntity.MaxLucky = 0;
        this.FishPrizeEntity = null;
    }
}
